// 引入模块
const mysql = require('mysql')
const {log} = console

// 暴露出去
module.exports = {
  db(response, sql, arr, callback) {
    let db = mysql.createConnection({
      host: 'localhost',
      port: 13306,
      user: 'root',
      password: 'abc123',
      database: 'studyroom'
    })
    db.connect()
    db.query(sql, arr, function (err, data) {
      log('链接数据库成功')
      if (!err) {
        callback(data)
      } else {
        response.send({
          code: 500,
          msg: '数据库操作失败',
          data: err
        })
      }
    })
    db.end()
  }
}